/***********************************************
 * Title:       AdmissionStateService.java
 * Description: AdmissionStateService.java
 * Author:      Niyuzhe
 * Create Date: 2010-1-18
 * CopyRight:   CopyRight(c)@2009
 * Company:     TJUSCS
 * Version:     1.0
 ***********************************************
 */
package edu.tju.runtimeConfig;

/**
 * @author Niyuzhe
 * 
 */
public class AdmissionStateService implements ConfigService
{
        
        public final static String AS_UNSTARTED = "unstarted";
        public final static String AS_AVAILABLE = "available";
        public final static String AS_OVER = "over";
        
        private final static String DB_KEY = "admissions";
        private final static String DEFAULT_VALUE = AS_UNSTARTED;

        
        
        private static AdmissionStateService _instance;
        static
        {
                _instance = new AdmissionStateService();
        }
        
        public static AdmissionStateService getInstance()
        {
                return _instance;
        }
        
        private AdmissionStateService(){}

        public String getAdmissionState()
        {
                try
                {
                        return RuntimeConfigUtil.getInstance().getValue(DB_KEY);
                } catch (NoSuchConfigInfoException e)
                {
                        RuntimeConfigUtil.getInstance().setValue(DB_KEY, DEFAULT_VALUE);
                        return DEFAULT_VALUE;
                }
        }

        public boolean isUnstarted()
        {
                return AS_UNSTARTED.equals(getAdmissionState());
        }
        
        public boolean isAvailable()
        {
                return AS_AVAILABLE.equals(getAdmissionState());
        }
        
        public boolean isOver()
        {
                return AS_OVER.equals(getAdmissionState());
        }
        
        public void setUnstarted()
        {
                RuntimeConfigUtil.getInstance().setValue(DB_KEY, AS_UNSTARTED);
        }
        
        public void setAvaiable()
        {
                RuntimeConfigUtil.getInstance().setValue(DB_KEY, AS_AVAILABLE);
        }
        
        public void setOver()
        {
                RuntimeConfigUtil.getInstance().setValue(DB_KEY, AS_OVER);
        }

        /* (non-Javadoc)
         * @see edu.tju.runtimeConfig.ConfigService#clearValue(java.lang.String)
         */
       
        public void clearValue(String newTerm)
        {
                if( TermService.getInstance().isNewEducationYear(newTerm) )
                {
                        setUnstarted();
                } else
                {
                        setOver();
                }
                
        }
}
